<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品详情</title>

    <link rel="stylesheet" href="../static/assets/global/plugins/bootstrap/css/bootstrap.min.css" type="text/css">
    <link rel="stylesheet" href="../static/assets/global/css/components.css" type="text/css">
    <link rel="stylesheet" href="../static/assets/admin/pages/css/login.css" type="text/css">
    <script src="../static/assets/global/plugins/jquery-1.11.0.min.js" type="text/javascript"></script>

</head>

<body class="login">
<div class="content">
    <h3 class="form-title">商品详情</h3>
    <div id="promoStartDateContainer" class="form-group">
        <!--<label style="color: blue;" id="promoStatus" class="control-label">秒杀开始时间</label>-->
        <div>
            <label style="color: red;" class="control-label" id="promoStartDate"></label>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label" id="title"></label>
    </div>
    <div class="form-group">
        <label class="control-label">描述</label>
        <div>
            <label class="control-label" id="description"></label>
        </div>
    </div>
    <div id="normalPriceContainer" class="form-group">
        <label class="control-label">价格</label>
        <div>
            <label class="control-label" id="price"></label>
        </div>
    </div>
    <div id="promoPriceContainer" class="form-group">
        <label style="color: red;" class="control-label">秒杀价格</label>
        <div>
            <label style="color: red;" class="control-label" id="promoPrice"></label>
        </div>
    </div>
    <div class="form-group">
        <img style="width: 200px; height: auto;" id="imgUrl">
    </div>
    <div class="form-group">
        <label class="control-label">库存</label>
        <div>
            <label class="control-label" id="stock"></label>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label">销量</label>
        <div>
            <label class="control-label" id="sales"></label>
        </div>
    </div>
    <div class="form-actions">
        <button class="btn blue" id="createOrder" type="submit">
            下单
        </button>
    </div>

</div>


<script>

    // JS获取URL？号后面的参数
    function getParam(paramName) {
        paramValue = "", isFound = !1;
        if (this.location.search.indexOf("?") === 0 && this.location.search.indexOf("=") > 1) {
            arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"), i = 0;
            while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() === paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++
        }
        return paramValue === "" && (paramValue = null), paramValue
    }

    let g_itemVo = {};

    $(function () {
        $("#createOrder").click(function () {
            $.ajax({
                type: "POST",
                url: "http://localhost:8090/order/createOrder",
                data: {
                    "itemId": g_itemVo.id,
                    "amount": 1,
                    "promoId": g_itemVo.promoId
                },
                xhrFields: {withCredentials: true},
                success: function (data) {
                    if (data.status === "success") {
                        alert("下单成功");
                        window.location.reload();
                    } else {
                        alert("下单失败，因为" + data.data.errMsg);
                        if (data.data.errCode === 20003) {
                            window.location.href = "login.html";
                        }
                    }
                },
                error: function (data) {
                    alert("下单信息失败，因为" + data.responseText);
                }
            });
        });

        // 获取商品详情
        $.ajax({
            type: "GET",
            url: "http://localhost:8090/item/get",
            data: {
                "id": getParam("id"),
            },
            xhrFields: {withCredentials: true},
            success: function (data) {
                if (data.status === "success") {
                    g_itemVo = data.data;
                    reloadDom();
                    // 定时器，刷新页面
                    setInterval(reloadDom, 1000);
                } else {
                    alert("获取信息失败，因为" + data.data.errMsg);
                }
            },
            error: function (data) {
                alert("获取信息失败，因为" + data.responseText);
            }
        });
    });

    function reloadDom() {
        $("#title").text(g_itemVo.title);
        $("#description").text(g_itemVo.description);
        $("#stock").text(g_itemVo.stock);
        $("#price").text(g_itemVo.price);
        $("#imgUrl").attr("src", g_itemVo.imgUrl);
        $("#sales").text(g_itemVo.sales);
        if (g_itemVo.promoStatus === 1) {
            // 秒杀活动还未开始

            //倒计时模块
            let startTime = g_itemVo.startDate.replace(new RegExp("-", "gm"), "/");
            startTime = (new Date(startTime).getTime());
            let nowTime = Date.parse(new Date());
            let delta = (startTime - nowTime) / 1000;
            if (delta <= 0) {
                // 活动开始了
                g_itemVo.promoStatus = 2;
                reloadDom();
            }

            $("#promoStartDate").text("秒杀活动将于：" + g_itemVo.startDate + " 开始售卖 倒计时：" + delta);
            $("#promoPrice").text(g_itemVo.promoPrice);
            // 秒杀活动还未开始，禁止下单
            $("#createOrder").attr("disabled", true);
        } else if (g_itemVo.promoStatus === 2) {
            // 秒杀活动正在进行中
            $("#promoStartDate").text("秒杀活动正在进行中");
            $("#promoPrice").text(g_itemVo.promoPrice);
            $("#createOrder").attr("disabled", false);
            $("#normalPriceContainer").hide();
        }
    }

</script>

</body>
</html>